package com.yiwei.ydd.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.PagesDetailBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.Util;

import org.sufficientlysecure.htmltextview.HtmlTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/4/24.
 */

public class PagesDetailDialog extends Dialog {

    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.txt_content)
    HtmlTextView txtContent;
    private Context context;
    private boolean isShow = false;
    private String keyword;

    public PagesDetailDialog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public PagesDetailDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_page_detail_tips);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        mWindow.setAttributes(lp);

//        getPagesDetail();
    }

    public PagesDetailDialog setKeyword(String keyword) {
        this.keyword = keyword;
        return this;
    }

    @Override
    public void show() {
        super.show();
        if (!isShow) {
            getPagesDetail();
        }
    }

    public void getPagesDetail() {
        if (!Network.checkNetwork(context)) {
            ToastUtil.show(context, "网络连接失败");
            return;
        }

        PagesDetailBean item = new PagesDetailBean();
        item.keyword = keyword;
        ((BaseActivity) context).loadingDialog.show();
        Api.api_service.getPagesDetail(item).compose(RxLifeUtil.checkOn(((BaseActivity) context))).
                doFinally(() -> {
                    try {
                        ((BaseActivity) context).loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    isShow = true;
                    txtTitle.setText(result.datas.title);
                    txtContent.setHtml(result.datas.content);
                });
    }

    private DialogDefaultClickListener listener;

    public PagesDetailDialog setDialogClickListener(DialogDefaultClickListener listener) {
        this.listener = listener;
        return this;
    }

    @OnClick({R.id.btn_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                dismiss();
                break;
        }
    }


    public interface DialogDefaultClickListener {
        public abstract void confirm(String money);
    }
}
